home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / test / mies.c < prev   
Encoding:
C/C++ Source or Header  |  1999-07-26  |  2.3 KB  |  100 lines

  1. /*
  2.   mies.c
  3.   
  4.   "Less is more." -- Mies van der Rowe.
  5.  
  6.   Demonstrate OpenBSD 2.4 < 4 byte last frag ENOBUF sendto() bug.
  7.  
  8.   the OpenBSD definition in <sys/param.h> for 2.4 was skipped!
  9.   code should check for OpenBSD < 199905 (2.5)...
  10.  
  11.   dugsong@monkey.org
  12.  
  13.   $Id: mies.c,v 1.2 1999/06/25 19:12:07 dugsong Exp $
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <netinet/in_systm.h>
  23. #include <netinet/ip.h>
  24. #include <netinet/tcp.h>
  25. #include <arpa/inet.h>
  26.  
  27. /* Configurable parameters. */
  28. #define TEST_PROTO    IPPROTO_TCP
  29. #define IP_SRC        "10.0.0.1"
  30. #define IP_DST        "192.0.0.1"
  31. #define FRAG_NUM    2
  32. #define FRAG_SZ        8
  33. #define LAST_SZ     1
  34.  
  35. /* Define BSD_BUG if testing on BSD/OS, etc. */
  36. #ifdef BSD_BUG
  37. #define FIX(x)    (x)
  38. #else
  39. #define FIX(x)    htons(x)
  40. #endif
  41.  
  42. int
  43. main()
  44. {
  45.   unsigned char buf[IP_MAXPACKET];
  46.   struct ip *iph;
  47.   struct sockaddr_in sin;
  48.   int sock, pktlen, offset, i, one = 1;
  49.   
  50.   iph = (struct ip *)buf;
  51.   pktlen = sizeof(struct ip) + FRAG_SZ;
  52.   
  53.   iph->ip_hl = 20 / 4;
  54.   iph->ip_v = 4;
  55.   iph->ip_tos = 0;
  56.   iph->ip_len = FIX(pktlen);
  57.   iph->ip_id = htons(getpid());
  58.   iph->ip_ttl = 255;
  59.   iph->ip_p = IPPROTO_TCP;
  60.   iph->ip_sum = 0;
  61.   iph->ip_src.s_addr = inet_addr(IP_SRC);
  62.   iph->ip_dst.s_addr = inet_addr(IP_DST);
  63.   
  64.   if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  65.     perror("socket");
  66.   
  67.   if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) == -1)
  68.     perror("setsockopt");
  69.   
  70.   memset(&sin, 0, sizeof(struct sockaddr_in));
  71.   sin.sin_family = AF_INET;
  72.   sin.sin_addr.s_addr = iph->ip_dst.s_addr;
  73.  
  74.   /* Send fragments. */
  75.   for (offset = 0 ; offset < FRAG_NUM ; offset++) {
  76.     iph->ip_off = FIX(IP_MF | offset);
  77.     
  78.     if (sendto(sock, buf, pktlen, 0, (struct sockaddr *)&sin,
  79.            sizeof(struct sockaddr)) != pktlen)
  80.       perror("sendto");
  81.  
  82.     printf(IP_SRC " > " IP_DST ": (frag %d:%d@%d+)\n",
  83.        iph->ip_id, FRAG_SZ, offset << 3);
  84.   }
  85.   /* Send last fragment. */
  86.   pktlen = sizeof(struct ip) + LAST_SZ;
  87.   iph->ip_len = FIX(pktlen);
  88.   iph->ip_off = FIX(IP_MF | offset);
  89.  
  90.   if (sendto(sock, buf, pktlen, 0, (struct sockaddr *)&sin,
  91.          sizeof(struct sockaddr)) != pktlen) {
  92.     perror("sendto");
  93.     printf("packet NOT sent: ");
  94.   }
  95.   printf(IP_SRC " > " IP_DST ": (frag %d:%d@%d)\n",
  96.      iph->ip_id, LAST_SZ, offset << 3);
  97.   
  98.   exit(0);
  99. }
  100.